/*
 * Created by SharpDevelop.
 * User: maplo
 * Date: 2/10/2010
 * Time: 10:44 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace Bulletin_Board
{
	/// <summary>
	/// Description of SendMessage.
	/// </summary>
	public class SendMessage
	{
		public string HostReceiver1;
		public string TextMessage1;
		
		public SendMessage(string HostReceiver, string TextMessage)
		{
			HostReceiver1 = HostReceiver;
			TextMessage1 = TextMessage;
		}
		
		public void SendMessageText()
		{		
			//Get IP address and start connection
			Socket Socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
			IPHostEntry IPEntry = Dns.GetHostEntry (HostReceiver1);
			IPAddress Addr1 = IPAddress.Parse(IPEntry.AddressList[0].ToString());
			IPEndPoint EPhost = new IPEndPoint( Addr1, 1224 );
			
			Net net = new Net();

			try{
				//Start connection
				Socket1.Connect(EPhost);
				
				//If connection established send data
				if (Socket1.Connected)
				{
					net.SendString( Socket1, TextMessage1);
					Socket1.Close();
				}
				else
				{
					Socket1.Close();
				}
			}
			catch (Exception e1)
			{
				//MessageBox.Show(e1.ToString());
				Socket1.Close();
			}
		}
	}
}
